package org.bonorsoft.modules.utils;

import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
import com.google.zxing.WriterException;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;

public class QrCodeUtil {

    public static String normal(String text) throws IOException, WriterException {
       return QrCodeGenWrapper.of(text)
               .setW(500)
               .setH(500)
               .asString();
    }

    public static String logo(String text, InputStream logoFile) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .setLogo(logoFile)
                .setLogoRate(7)
                .setLogoStyle(QrCodeOptions.LogoStyle.ROUND)
                .asString();
    }

    public static String color(String text) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .setDrawPreColor(Color.GREEN)
                .setW(500)
                .setH(500)
                .asString();
    }

    public static String background(String text, InputStream bgFile) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .setBgImg(bgFile)
                .setBgStyle(QrCodeOptions.BgImgStyle.PENETRATE)
                .setBgW(500)
                .setBgH(500)
                .setW(500)
                .setH(500)
                /*.setBgStartX(500)
                .setBgStartY(500)*/
//                .setBgOpacity(0.5f)
                .asString();
    }

    public static String style(String text) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .setBgH(500)
                .setBgW(500)
                .setH(500)
                .setW(500)
                .setDrawEnableScale(true)
                .setDrawStyle(QrCodeOptions.DrawStyle.CIRCLE)
                .asString();
    }

    public static String imageFill(String text, InputStream bgFile) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .setW(500)
                .setH(500)
                .setDrawEnableScale(true)
                .setErrorCorrection(ErrorCorrectionLevel.H)
                .setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)
                .addImg(1, 1, bgFile)
                .asString();
    }

    public static String gif(String text, InputStream bgFile) throws IOException, WriterException {
        return QrCodeGenWrapper.of(text)
                .setW(500)
                .setH(500)
                .setBgImg(bgFile)
                .setBgOpacity(0.5f)
                .setPicType("gif")
                .asString();
    }

    public static String colours(String skipLink) throws IOException, WriterException {
        return QrCodeGenWrapper.of(skipLink)
                .setW(500)
                // 探测图形特殊处理
                .setDetectSpecial()
                // 定位点(探测图形)外边颜色
                .setDetectOutColor(Color.CYAN)
                // 定位点内部颜色
                .setDetectInColor(Color.RED)
                // 二维码着色点
                .setDrawPreColor(Color.GREEN)
                // 二维码背景图
                .setDrawBgColor(0xffffffff)
                .asString();
    }
}
